<?php
use yii\bootstrap\ActiveForm;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;
?>
<?php $this->beginPage()?>
    <!DOCTYPE html>
    <html lang="<?= Yii::$app->language ?>">
    <head>
        <meta charset="<?= Yii::$app->charset ?>" />
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <?= Html::csrfMetaTags()?>
        <title><?= Html::encode($this->title) ?></title>
        <script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"
                type="text/javascript"></script>
        <?php $this->head()?>
    </head>
    <body class="hold-transition skin-blue sidebar-mini">
    <?php $this->beginBody()?>
    <bgsound src="D:\AudioFile\" />
    <div style="padding:10px">
        <div class="wrapper">
            <?= $this->render('/common/dialog')?>
            <?= $this->render('/common/alert')?>
            <!-- 	查询开始与结束的日期 -->
            <div class="form-group">
                <!-- 模型的选择 -->
                <?= $this->render('/common/widget')?>
                <?php $form = ActiveForm::begin(['action'=>'']); ?>
                <?=$form->field($arcTrainModel, 'arc_classify')->dropDownList(
                    ArrayHelper::map($arcClassifys, 'id', 'name'),
                    ['value'=>$arcTrainModel->arc_classify,'disabled'=>'disabled']//默认选中当前的目标,且不能修改
                )->label('选择模型');?>
                <?=$form->field($arcTrainModel, 'arc_android')->dropDownList(
                    ArrayHelper::map($arcAndroids, 'id', 'name'),
                    ['value'=>$arcTrainModel->arc_android,'disabled'=>'disabled']//默认选中当前的目标,且不能修改
                )->label('选择机器人');?>
                <p>点击该按钮后会根据选择的模型以及机器人进行分类训练：</p>
                <!-- 会新增arc_train 训练表表示用了哪个机器人，以及arc_progress来表示训练的进度 -->
                <input class='btn btn-primary' type='button' id='train' value='继续训练' />
                <input class='btn btn-warning' type='button' id='finishTrain' value='中止训练' />
                <?php ActiveForm::end(); ?>
            </div>
        </div>
    </div>
    <?php $this->endBody()?>
    </body>
    <script type="text/javascript">
        var url = "classify-continue";
        var _start_i = 0;
        var onetimeNum = <?= isset($onetimeNum)?$onetimeNum:0 ?>;
        var back = '<span class="sr-only">100% Complete</span>';
        var arcTrainId = <?= isset($arcTrainModel->id)?$arcTrainModel->id:0 ?>;
        var arcTrainProgress_num = <?= isset($arcTrainModel->progress_num)?$arcTrainModel->progress_num:0 ?>;
        var arcTrainStart = false;//第一次进入
        var continueTrain = false;//按钮变成了继续训练
        $("#loading div div").attr('style','width:'+(arcTrainProgress_num/onetimeNum*100)+'%;color:grey');//让正在加载的页面显示,这里的20为
        $("#loading div div").html((arcTrainProgress_num/onetimeNum*100).toFixed(2)+'% 识别完第'+arcTrainProgress_num+'条 '+back);
        <?php
                if ($arcTrainModel->progress==3){//已被中止
        ?>
            $("#finishTrain").attr("disabled","disabled");
            $("#train").removeAttr("disabled");
        <?php
              }else if($arcTrainModel->progress==1){//正在训练
         ?>
            $("#train").attr("disabled","disabled");
            $("#finishTrain").removeAttr("disabled");
        <?php
              }
            ?>
        var lastData = null;
        $("#train").click(function(){
            continueTrain = true;
            $("#train").attr("disabled","disabled");
            $("#finishTrain").removeAttr("disabled");
            $("#arctrain-arc_classify").attr("disabled","disabled");
            $("#arctrain-arc_android").attr("disabled","disabled");
            $("#uploading div div").html("正在上传文件中，请等待...");
            $("#uploading").removeAttr('hidden');//让正在上传文件的内容显示
            $("#faild").attr('hidden','hidden');//让正在加载的页面隐藏
            $("#success").attr('hidden','hidden');//让正在加载的页面隐藏
            $.ajax({
                type:"post",
                async:true,
                cache:false,
                url:url,/*url写异域的请求地址*/
                data:{
                    startI:_start_i,
                    arcTrainId:arcTrainId,//每个机器人训练的编号，用以区分是来自哪个训练
                    arcClassify:$('#arctrain-arc_classify').find("option:selected").attr('value'),
                    arcAndroid:$('#arctrain-arc_android').find("option:selected").attr('value'),
                    arcTrainStart:arcTrainStart,
                    continueTrain:continueTrain
                },
                success: function(datas){
                    lastData = datas;
                    arcTrainStart = false;
                    var datasObj = JSON.parse(datas);
                    if((typeof datasObj) == "object"){
                        arcTrainId = datasObj.arcTrainId;
                        _start_i = parseInt(datasObj.arcTrainProgressNum);
                        onetimeNum = parseInt(datasObj.onetimeNum);
                    }else if((typeof datasObj) == "number"){
                        _start_i = parseInt(datas);
                    }
                    if(_start_i>=0){
                        $("#loading div div").attr('style','width:'+(_start_i/onetimeNum*100)+'%;color:grey');//让正在加载的页面显示,这里的20为
                        $("#loading div div").html((_start_i/onetimeNum*100).toFixed(2)+'% 识别完第'+_start_i+'条 '+back);
                        if(_start_i/onetimeNum*100>=100){
                            $("#uploading").attr('hidden','hidden');//让正在上传文件的内容隐藏
                            $("#success").removeAttr('hidden');
                            $("#finishTrain").attr("disabled","disabled");
                        }else{
                            ajaxTo(_start_i);
                        }
                    }else{
                        errorNotice(_start_i);
                    }
                },
                error: function(error){
                    if(lastData!=null) {
                        var datas = lastData;
                        var datasObj = JSON.parse(datas);
                        if((typeof datasObj) == "object"){
                            arcTrainId = datasObj.arcTrainId;
                            _start_i = parseInt(datasObj.arcTrainProgressNum);
                            onetimeNum = parseInt(datasObj.onetimeNum);
                        }else if((typeof datasObj) == "number"){
                            _start_i = parseInt(datas);
                        }
                        if(_start_i>=0){
                            $("#loading div div").attr('style','width:'+(_start_i/onetimeNum*100)+'%;color:grey');//让正在加载的页面显示,这里的20为
                            $("#loading div div").html((_start_i/onetimeNum*100).toFixed(2)+'% 识别完第'+_start_i+'条 '+back);
                            if(_start_i/onetimeNum*100>=100){
                                $("#uploading").attr('hidden','hidden');//让正在上传文件的内容隐藏
                                $("#success").removeAttr('hidden');
                                $("#finishTrain").attr("disabled","disabled");
                            }else{
                                ajaxTo(_start_i);
                            }
                        }else{
                            errorNotice(_start_i);
                        }
                    }else{
                        $("#uploading div div").html("正在上传文件中，请等待...");
                        $("#uploading").attr('hidden', 'hidden');//让正在上传文件的内容隐藏
                        $("#faild").removeAttr('hidden');//让失败的页面显示
                    }
                }
            });

        });
        $("#finishTrain").click(function(){//终止训练
            continueTrain = false;
            var url = "classify-stop";
            $("#finishTrain").attr("disabled","disabled");
            $("#uploading div div").html("正在中止上传识别");
            $.ajax({
                type:"post",
                url:url,/*url写异域的请求地址*/
                data:{
                    arcTrainId:arcTrainId
                },
                success: function(datas){
                    var notice = "";
                    if(datas == "-5"){//保存成功
                        notice = "中止成功！";
                        $("#train").removeAttr("disabled");
                    }else{//保存失败
                        notice = "中止失败！";
                    }
                    krajeeDialog.alert(notice);
                },
                error: function(error){
                    var notice = "中止失败，请点击确定继续上传识别！";
                    krajeeDialog.alert(notice);
                }
            });
        });
        function ajaxTo(id){
            $.ajax({
                type:"post",
                async:true,
                cache:false,
                url:url,/*url写异域的请求地址*/
                data:{
                    startI:id,
                    arcTrainId:arcTrainId,//每个机器人训练的编号，用以区分是来自哪个训练
                    arcClassify:$('#arctrain-arc_classify').find("option:selected").attr('value'),
                    arcAndroid:$('#arctrain-arc_android').find("option:selected").attr('value'),
                    continueTrain:continueTrain
                },
                success: function(datas){
                    lastData = datas;
                    var datasObj = JSON.parse(datas);
                    if((typeof datasObj) == "object"){
                        arcTrainId = datasObj.arcTrainId;
                        id = parseInt(datasObj.arcTrainProgressNum);
                        onetimeNum = parseInt(datasObj.onetimeNum);
                    }else if((typeof datasObj) == "number"){
                        id = parseInt(datas);
                    }
                    if(id>=0){
                        $("#loading div div").attr('style','width:'+(id/onetimeNum*100)+'%;color:grey');//让正在加载的页面显示,这里的20为
                        $("#loading div div").html((id/onetimeNum*100).toFixed(2)+'% 识别完第'+id+'条 '+back);
                        if(id/onetimeNum*100>=100){
                            $("#uploading").attr('hidden','hidden');//让正在上传文件的内容隐藏
                            $("#success").removeAttr('hidden');
                            $("#finishTrain").attr("disabled","disabled");
                        }else{
                            setTimeout(ajaxTo(id), 100);//定时器设置10mms后就执行
                        }
                    }else{
                        errorNotice(id);
                    }
                },
                error: function(error){
                    if(lastData!=null){
                        var datas = lastData;
                        var datasObj = JSON.parse(datas);
                        if((typeof datasObj) == "object"){
                            arcTrainId = datasObj.arcTrainId;
                            id = parseInt(datasObj.arcTrainProgressNum);
                            onetimeNum = parseInt(datasObj.onetimeNum);
                        }else if((typeof datasObj) == "number"){
                            id = parseInt(datas);
                        }
                        if(id>=0){
                            $("#loading div div").attr('style','width:'+(id/onetimeNum*100)+'%;color:grey');//让正在加载的页面显示,这里的20为
                            $("#loading div div").html((id/onetimeNum*100).toFixed(2)+'% 识别完第'+id+'条 '+back);
                            if(id/onetimeNum*100>=100){
                                $("#uploading").attr('hidden','hidden');//让正在上传文件的内容隐藏
                                $("#success").removeAttr('hidden');
                                $("#finishTrain").attr("disabled","disabled");
                            }else{
                                setTimeout(ajaxTo(id), 100);//定时器设置10mms后就执行
                            }
                        }else{
                            errorNotice(id);
                        }
                    }else{
                        $("#uploading").attr('hidden','hidden');//让正在上传文件的内容隐藏
                        $("#faild").removeAttr('hidden');//让失败的页面显示
                    }
                }
            });

        };

        //错误提示
        function errorNotice(id){
            var error = '';
            $("#faild").removeAttr('hidden');
            //-1,最大的次数超过默认的500次或者onetimeNum次；
            //-2，单条训练数据保存失败
            //-3,保存进度出错！，
            //-4,单个机器人不能创建太多的机器人副本
            //-5,主动关闭的训练
            switch(id){
                case -1:
                    error = '最大的次数超过'+onetimeNum+'次';
                    break;
                case -2:
                    error = '单条训练数识别失败，请确认文件有效据保存失败';
                    break;
                case -3:
                    error = '进度保存出错';
                    break;
                case -4:
                    error = '单个机器人不能创建太多的机器人副本，请重新选择机器人';
                    arcTrainStart = true;
                    $("#arctrain-arc_android").removeAttr("disabled");
                    break;
                case -5:
                    error = '主动关闭的训练';
                    $("#train").removeAttr("diabled");
                    $("#train").val("继续训练");
                    $("#continueTrain").removeAttr("diabled");
                    break;
                case -6:
                    error = '请填写正确的机器人参数';
                    break;
                case -7:
                    error = '您已经训练完成了所有的文件，无需再进行训练';
                    break;

            }
            $("#uploading").attr('hidden','hidden');//让正在上传文件的内容隐藏
            $("#faild div div").html(error+back);
            $("#finishTrain").attr("disabled","disabled");
            $("#train").removeAttr("disabled");
        }

        function readDb(){
            var url = "refresh";
            $.ajax({
                type:"post",
                async:true,
                cache:false,
                url:url,/*url写异域的请求地址*/
                data:{
                    arcTrainId:arcTrainId,//每个机器人训练的编号，用以区分是来自哪个训练
                },
                success: function(datas){
                    var id = 0;
                    var datasObj = JSON.parse(datas);
                    if((typeof datasObj) == "object"){
                        arcTrainId = datasObj.arcTrainId;
                        id = parseInt(datasObj.arcTrainProgressNum);
                        onetimeNum = parseInt(datasObj.onetimeNum);
                    }else if((typeof datasObj) == "number"){
                        id = parseInt(datas);
                    }
                    if(id>=0){
                        $("#loading div div").attr('style','width:'+(id/onetimeNum*100)+'%;color:grey');//让正在加载的页面显示,这里的20为
                        $("#loading div div").html((id/onetimeNum*100).toFixed(2)+'% 识别完第'+id+'条 '+back);
                        if(id/onetimeNum*100>=100){
                            $("#uploading").attr('hidden','hidden');//让正在上传文件的内容隐藏
                            $("#success").removeAttr('hidden');
                            $("#finishTrain").attr("disabled","disabled");
                        }
                    }else{
                        errorNotice(id);
                    }
                },
                error: function(error){
                    $("#uploading").attr('hidden','hidden');//让正在上传文件的内容隐藏
                    $("#faild").removeAttr('hidden');//让失败的页面显示
                }
            });

        };

        function refresh(){
            location.reload();
        }
        setInterval(readDb, 3000);
    </script>
    </html>
<?php $this->endPage()?>